/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.hauntedhousesweb.web;

import cz.muni.fi.hauntedhouses.service.SkillService;
import cz.muni.fi.hauntedhouses.to.SkillTO;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

/**
 *
 * @author Wendy
 */
@UrlBinding("/skills/{$event}/{skill.id}") 
public class SkillsActionBean implements ActionBean{
    
    private ActionBeanContext context;
    
    @SpringBean
    private SkillService skillService;

    @DefaultHandler
    public Resolution all() {
        if (skillService == null) {
            return new ForwardResolution("/jetozle.jsp");
        }
        return new ForwardResolution("/showSkills.jsp"); 
    }
    
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }
    
    public List<SkillTO> getSkills() {
        return skillService.findAll();
    }
    
    
    @ValidateNestedProperties(value = {
    @Validate(on = {"add", "save"}, field = "name", required = true),
    @Validate(on = {"add", "save"}, field = "causesFear", required = true),
    @Validate(on = {"add", "save"}, field = "power", required = true, minvalue=0, maxvalue=100),
    @Validate(on = {"add", "save"}, field = "description", minlength=0, maxlength=50)            
    })
    private SkillTO skill;
 
    public Resolution add() {
        skillService.create(skill);
        return new RedirectResolution(this.getClass(), "all");
    }
 
    public SkillTO getSkill() {
        return skill;
    }
 
    public void setSkill(SkillTO skill) {
        this.skill = skill;
    }
    
    public Resolution delete() {
        skillService.delete(skill);        
        return new RedirectResolution(this.getClass(), "all");
    }
    
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadSkillFromDatabase() {
        String ids = context.getRequest().getParameter("skill.id");
        if (ids != null) {
            skill = skillService.get(Long.parseLong(ids));
        }
    }
 
    public Resolution edit() {
        return new ForwardResolution("/editSkill.jsp");
    }
 
    public Resolution save() {
        skillService.update(skill);
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution cancel() {
        return new RedirectResolution(this.getClass(), "all");
    }
    
}

